package com.ziwei.material.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;


import java.io.InputStream;

/**
 * @author: jl_zhang
 * @since: 2024/11/18 10:23
 **/

//阿里OSS
public class AliOssUtil {

    //设置EndPoint
    private static final String ENDPOINT = "https://oss-cn-hangzhou.aliyuncs.com";

    private static final String ACCESS_KEY_ID = "LTAI5tSM1vEX1ARhGYbNNLit";

    private static final String ACCESS_KEY_SECRET = "UIJDnrJgBieGfLvAF5PdrPdOZ34q6B";

    //桶名称
    private static final String BUCKET_NAME = "sds-material-tool";

    public static String uploadImage(String objectName, InputStream in){
        //创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(
                ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET
        );

        String url = "";
        try{
            //创建PutObjectRequest对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(
                    BUCKET_NAME, objectName, in
            );

            //上传文件
            ossClient.putObject(putObjectRequest);

            url = "https://" + BUCKET_NAME + "." +
                    ENDPOINT.substring(ENDPOINT.lastIndexOf("/") + 1)
                    + "/" + objectName;


        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            if (ossClient != null){
                ossClient.shutdown();
            }
        }

        return url;
    }

}
